// pages/course/grade/grade.js
const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stars: [0, 1, 2, 3, 4],

    normalSrc: '../../../images/start-off.png',

    selectedSrc: '../../../images/start-on.png',

    halfSrc: '../../../images/start-half.png',

    key: 3.5,//评分,
    rated: false,
    begin: 0,
    length: 10,
    more: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      courseSchedules: [],
      begin: 0,
      length: 10
    })
    this.listSchedule();
  },

  listSchedule: function() {
    wx.showLoading({
      title: "正在加载中..."
    })
    var that = this;
    wx.request({
      url: that.data.host + '/coach/course-grades' + "?mobile=" + that.data.mobile + "&rated=" + that.data.rated + "&begin=" + that.data.begin + '&length=' + that.data.length,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          for (let i in res.data.data.courseSchedules) {
            that.data.courseSchedules.push(res.data.data.courseSchedules[i]);
          }
          if (res.data.data.courseSchedules.length < that.data.length) {
            that.setData({
              more: false
            })
          }
          that.setData({
            courseSchedules: that.data.courseSchedules,
            begin: that.data.begin + that.data.length
          });
          wx.hideLoading();
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.more) {
      this.listSchedule();
    }
  },

  switchTap: function() {
    this.setData({
      rated: !this.data.rated,
      courseSchedules: [],
      begin: 0,
      length: 10
    })
    this.listSchedule();
  },

  //点击左边,半颗星

  selectLeft: function (e) {
    var key = e.currentTarget.dataset.key
    if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {
      key = 0;
    }
    console.log("得" + key + "分")
    this.setData({
      key: key
    })
  },
  //点击右边,整颗星

  selectRight: function (e) {
    var key = e.currentTarget.dataset.key
    console.log("得" + key + "分")
    this.setData({
      key: key
    })
  }
})